package topcbl.adapter;

import java.util.ArrayList;

import topcbl.taxi.MainActivity;
import topcbl.taxi.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class AutoCompleteTextAdapter extends ArrayAdapter<String> {
	Context context;
	int resource;
	ArrayList<String> data;

	public AutoCompleteTextAdapter(Context context, int resource,
			ArrayList<String> data) {
		super(context, resource, data);
		this.context = context;
		this.resource = resource;
		this.data = data;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = LayoutInflater.from(context);
		convertView = inflater.inflate(resource, null);
		TextView tv = (TextView) convertView.findViewById(R.id.textView1);
		tv.setText(data.get(position));
		convertView.setOnClickListener(new OnItemClickListener(position));
		return convertView;
	}

	private class OnItemClickListener implements OnClickListener {
		private int mPosition;

		OnItemClickListener(int position) {
			mPosition = position;
		}

		@Override
		public void onClick(View arg0) {
			MainActivity activity = ((MainActivity) context);
			activity.setAddress(data.get(mPosition));
			activity.moveMap();
		}
	}
}
